package org.fuys.coder.application.admin;

import org.fuys.coder.common.entity.Result;
import org.fuys.coder.domain.auth.model.annotation.PermissionCertify;
import org.fuys.coder.domain.auth.model.vo.permission.PermissionTypeVO;
import org.fuys.coder.domain.substance.model.req.admin.SubstanceAdminModifyReq;
import org.fuys.coder.domain.substance.model.req.admin.SubstanceAdminSelectReq;
import org.fuys.coder.domain.substance.model.vo.SubstanceStatusTypeVO;
import org.fuys.coder.domain.substance.model.vo.SubstanceVO;
import org.fuys.coder.domain.substance.service.repo.ISubstanceService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.application.admin
 * @className: SubstanceAdminController
 * @author: WangJing
 * @description: 内容管理控制器
 * @date: 2024/8/17 16:03
 * @version: 1.0
 */

@RestController
@RequestMapping("/fuyscoder/admin/substance")
public class SubstanceAdminController {

    @Resource
    private ISubstanceService substanceService;

    @PermissionCertify(permission = PermissionTypeVO.SUBSTANCE_SELECT)
    @PostMapping("/select")
    public Result selectSubstance(@RequestBody SubstanceAdminSelectReq req){
        try {
            if(req.getId()!=null){
                SubstanceVO vo=substanceService.getSubstanceById(req.getId());
                if(!ObjectUtils.isEmpty(vo)){
                    return Result.ok().data(Collections.singletonList(vo));
                }
            }else if(req.getTitle()!=null){
                final List<SubstanceVO> substanceByName = substanceService.getSubstanceByName(req.getPageDTO(), req.getTitle());
                return Result.ok().data(substanceByName);
            }
        } catch (Exception e) {
            return Result.error("服务器端未知错误:"+e.getCause());
        }
        return Result.ok();
    }

    @PermissionCertify(permission = PermissionTypeVO.SUBSTANCE_MODIFY)
    @PostMapping("/modify")
    public Result modifySubstance(@RequestBody SubstanceAdminModifyReq req){
        try {
            if(req.getStatus()!=null) {
                substanceService.modifySubstanceStatus(req.getId(),req.getStatus(), SubstanceStatusTypeVO.AUDIT);
            }
        } catch (Exception e) {
            return Result.error("服务器端未知错误:"+e.getCause());
        }
        return Result.ok();
    }
}
